package legumes.client.ui.otherboxes;

import legumes.client.evt.main.UserConnectedEvent;
import legumes.client.evt.main.UserConnectedEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEvent;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEventHandler;
import legumes.client.resources.PNResources;
import legumes.shared.domain.Receipe;
import legumes.shared.domain.SellSessionForCustomer;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ReceipesBoxPanel extends Window {

	private ListGrid receipeGrid;
	
	public ReceipesBoxPanel() {
		super();
		setWidth100();
		setHeight(100);
		setMargin(5);
		
		setTitle("<img src=\"" + PNResources.INSTANCE.receipeIcon().getURL() + "\" />&nbsp;Recettes de la semaine");
		
		setShowCloseButton(false);
        setShowMinimizeButton(true);
		setCanDrag(false);
		setCanDragReposition(false);
		
		receipeGrid = new ListGrid();  
        receipeGrid.setWidth100();  
        receipeGrid.setHeight100(); 
        receipeGrid.setShowAllRecords(true);  
        receipeGrid.setBodyOverflow(Overflow.VISIBLE);  
        receipeGrid.setOverflow(Overflow.VISIBLE);  
        receipeGrid.setLeaveScrollbarGap(false); 
        
        ListGridField idField = new ListGridField("receipeId", "id", 0);
        idField.setHidden(true);
        idField.setType(ListGridFieldType.FLOAT);
        
        ListGridField titleField = new ListGridField("title", "Titre", 50);
        titleField.setAlign(Alignment.CENTER);
        
        ListGridField textField = new ListGridField("text", "Recette");  
        textField.setAlign(Alignment.CENTER);
        
        receipeGrid.setFields(idField, titleField, textField);  
        bind();
        setVisible(false);
		addItem(receipeGrid);
	}
	
	private void bind(){
		EventBusManager.getInstance().getEventBus().addHandler(
				PlaceDeliverySelectionChangedEvent.TYPE, 
				new PlaceDeliverySelectionChangedEventHandler(){

					@Override
					public void onStoreSelectionChanged(
							PlaceDeliverySelectionChangedEvent event) {
						if (event.getStore() != null){
							ReceipesBoxPanel.this.show();
						} else {
							ReceipesBoxPanel.this.hide();
						}
					}
					
				});
		
		EventBusManager.getInstance().getEventBus().addHandler(
				UserConnectedEvent.TYPE, 
				new UserConnectedEventHandler(){

					@Override
					public void onUserConnected(UserConnectedEvent event) {
						if( ! isVisible() ){
							setVisible(true);
						}
					}
				});
	}
	
	public void initData(SellSessionForCustomer ssfc_){
		if( ssfc_ != null && ssfc_.getStall() != null){
			ListGridRecord [] records = new ListGridRecord[ssfc_.getStall().getReceipes().size()];
			int i=0;
			for (Receipe r : ssfc_.getStall().getReceipes()) {
				ListGridRecord record = new ListGridRecord();
				record.setAttribute("receipeId", r.getId());
				record.setAttribute("title", r.getName());
				record.setAttribute("text", r.getText());
				records[i] = record;
				i++;
			}
			
			receipeGrid.setRecords(records);
		}
	}

}
